﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Medical.EntityType;
using Sector7.Medical.Events;
using Sector7.Framework.Enums;
using Sector7.Medical.Consts;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// RecipeControl.xaml 的交互逻辑
    /// </summary>
    public partial class RecipeControl : AddModeControlBase
    {
        #region ============================================== Accessor
        /// <summary>
        /// 获取和设置处方单控件信息
        /// </summary>
        public M7_Recipe Recipe
        {
            get { return m_entity as M7_Recipe; }
            set { m_entity = value; }
        }
        #endregion

        #region ============================================== Public functions
        /// <summary>
        /// 获取当前处方单信息
        /// </summary>
        /// <returns></returns>
        public M7_Recipe GetRecipe()
        {
            if (Recipe.Items == null)
            {
                Recipe.Items = new List<M7_RecipeItem>();
            }

            foreach (var itemControl in stpMain.Children)
            {
                var recipeItem = (itemControl as RecipeItemControl).GetRecipeItem();
                recipeItem.RecipeId = Recipe.RecipeId;

                Recipe.Items.Add(recipeItem);
            }

            return Recipe;
        }

        /// <summary>
        /// 设置当前处方单信息
        /// </summary>
        /// <param name="recipe"></param>
        public void SetRecipe(M7_Recipe recipe)
        {
            Recipe = recipe;

            if (recipe.Items != null)
            {
                stpMain.Children.Clear();
                for (int i = 0; i < Recipe.Items.Count; i++)
                {
                    AddRecipeItemControlToPanel(CreateRecipeItemControl(i + 1, Recipe.Items[i]), false);
                }
            }
        }
        #endregion

        #region ============================================== Private Functions
        /// <summary>
        /// 添加处方单项控件到面板
        /// </summary>
        /// <param name="itemControl">处方单项控件</param>
        /// <param name="isClear">是否清除已有处方单项控件</param>
        private void AddRecipeItemControlToPanel(RecipeItemControl itemControl, bool isClear)
        {
            if (isClear)
            {
                stpMain.Children.Clear();
            }
            stpMain.Children.Add(itemControl);
        }

        /// <summary>
        /// 创建处方单项控件
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        private RecipeItemControl CreateRecipeItemControl(int no)
        {
            var recipeItemControl = new RecipeItemControl();
            recipeItemControl.No = no;
            recipeItemControl.EditCompleted += new EditCompletedEventHandler(RecipeItemControl_EditCompleted);

            return recipeItemControl;
        }

        /// <summary>
        /// 创建处方单项控件
        /// </summary>
        /// <param name="no"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        private RecipeItemControl CreateRecipeItemControl(int no, M7_RecipeItem item)
        {
            var recipeItemControl = CreateRecipeItemControl(no);
            //recipeItemControl.RecipeItem = item;
            recipeItemControl.SetRecipeItem(item);

            return recipeItemControl;
        }
        #endregion

        #region ============================================== Event Functions
        /// <summary>
        /// 加载控件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddModeControlBase_Loaded(object sender, RoutedEventArgs e)
        {
            AddRecipeItemControlToPanel(CreateRecipeItemControl(1), true);
        }

        /// <summary>
        /// 处方项编辑完成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RecipeItemControl_EditCompleted(object sender, EditCompletedEventArgs e)
        {
            switch (e.CrudType)
            {
                case CrudType.Create:
                    int m_len = stpMain.Children.Count + 1;
                    if (m_len <= C_MaxSize.RECIPE_ITEM_MAX_SIZE)
                    {
                        AddRecipeItemControlToPanel(CreateRecipeItemControl(m_len), false);
                    }
                    else
                    {
                        //提示错误操作
                        //var recipeItem1 = sender as RecipeItemControl;
                        //Recipe.Items.Add(recipeItem1.RecipeItem);
                    }
                    break;
                case CrudType.Delete:
                    var recipeItem = sender as RecipeItemControl;
                    if (recipeItem != null)
                    {
                        stpMain.Children.Remove(recipeItem);

                        foreach (var items in stpMain.Children)
                        {
                            int no = (items as RecipeItemControl).No;
                            if (no >= 2)
                            {
                                no -= 1;
                                (items as RecipeItemControl).No = no;
                            }
                        }

                        if (stpMain.Children.Count == 0)
                        {
                            AddRecipeItemControlToPanel(CreateRecipeItemControl(1), true);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region ============================================== Structure
        public RecipeControl()
        {
            InitializeComponent();
        }
        #endregion

        #region ============================================== Inner members
        #endregion
    }
}
